1 Imports System.Data.SqlClient
2 Public Class frmDesignation
3
4     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
5         Me.Close()
6     End Sub
7     Sub Reset()
8         txtDesignation.Text =
""
9         btnSave.Enabled = True
10         btnDelete.Enabled = False
11         btnUpdate.Enabled = False
12         txtDesignation.Focus()
13     End Sub
14     Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
15         Reset()
16     End Sub
17
18     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
19         If txtDesignation.Text =
"" Then
20             MessageBox.Show(
"Please enter Designation", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
21             txtDesignation.Focus()
22             Return
23         End If
24
25         Try
26             con = New SqlConnection(cs)
27             con.Open()
28             Dim ct As String =
"select Designation from Designation where Designation=@d1"
29             cmd = New SqlCommand(ct)
30             cmd.Parameters.AddWithValue(
"@d1", txtDesignation.Text)
31             cmd.Connection = con
32             rdr = cmd.ExecuteReader()
33
34             If rdr.Read() Then
35                 MessageBox.Show(
"Designation Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
36                 txtDesignation.Text =
""
37                 txtDesignation.Focus()
38                 If (rdr IsNot Nothing) Then
39                     rdr.Close()
40                 End If
41                 Return
42             End If
43
44             con = New SqlConnection(cs)
45             con.Open()
46
47             Dim cb As String =
"insert into Designation(Designation) VALUES (@d1)"
48             cmd = New SqlCommand(cb)
49             cmd.Parameters.AddWithValue(
"@d1", txtDesignation.Text)
50             cmd.Connection = con
51             cmd.ExecuteReader()
52             con.Close()
53             Dim st As String =
"added the new Designation '" & txtDesignation.Text & "'"
54             LogFunc(lblUser.Text, st)
55             MessageBox.Show(
"Successfully Saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
56             btnSave.Enabled = False
57             Getdata()
58         Catch ex As Exception
59             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
60         End Try
61     End Sub
62
63     Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
64         If txtDesignation.Text =
"" Then
65             MessageBox.Show(
"Please enter Designation", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
66             txtDesignation.Focus()
67             Return
68         End If
69
70         Try
71
72             con = New SqlConnection(cs)
73             con.Open()
74
75             Dim cb As String =
"Update Designation set Designation=@d1 where Designation=@d2"
76             cmd = New SqlCommand(cb)
77             cmd.Connection = con
78             cmd.Parameters.AddWithValue(
"@d1", txtDesignation.Text)
79             cmd.Parameters.AddWithValue(
"@d2", txtDG.Text)
80             cmd.ExecuteReader()
81             con.Close()
82             Dim st As String =
"updated the Designation '" & txtDesignation.Text & "'"
83             LogFunc(lblUser.Text, st)
84             MessageBox.Show(
"Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
85             btnUpdate.Enabled = False
86             Getdata()
87         Catch ex As Exception
88             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
89         End Try
90     End Sub
91     Private Sub DeleteRecord()
92
93         Try
94             Dim RowsAffected As Integer =
0
95             con = New SqlConnection(cs)
96             con.Open()
97             Dim cl As String =
"select Designation.Designation from Staff,Designation where Staff.Designation=Designation.Designation and Designation.Designation=@d1"
98             cmd = New SqlCommand(cl)
99             cmd.Connection = con
100             cmd.Parameters.AddWithValue(
"@d1", txtDesignation.Text)
101             rdr = cmd.ExecuteReader()
102             If rdr.Read Then
103                 MessageBox.Show(
"Unable to delete..Already in use in Staff Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
104                 If Not rdr Is Nothing Then
105                     rdr.Close()
106                 End If
107                 Exit Sub
108             End If
109             con = New SqlConnection(cs)
110             con.Open()
111             Dim cq As String =
"delete from Designation where Designation=@d1"
112             cmd = New SqlCommand(cq)
113             cmd.Parameters.AddWithValue(
"@d1", txtDG.Text)
114             cmd.Connection = con
115             RowsAffected = cmd.ExecuteNonQuery()
116             If RowsAffected >
0 Then
117                 Dim st As String =
"deleted the Designation '" & txtDesignation.Text & "'"
118                 LogFunc(lblUser.Text, st)
119                 MessageBox.Show(
"Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
120                 Getdata()
121                 Reset()
122             Else
123                 MessageBox.Show(
"No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
124                 Reset()
125             End If
126             If con.State = ConnectionState.Open Then
127                 con.Close()
128
129             End If
130         Catch ex As Exception
131             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
132         End Try
133     End Sub
134     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
135         Try
136             If MessageBox.Show(
"Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
137                 DeleteRecord()
138             End If
139         Catch ex As Exception
140             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
141         End Try
142     End Sub
143
144     Private Sub dgw_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
145         Try
146             Dim dr As DataGridViewRow = dgw.SelectedRows(
0)
147             txtDG.Text = dr.Cells(
0).Value.ToString()
148             txtDesignation.Text = dr.Cells(
0).Value.ToString()
149             btnUpdate.Enabled = True
150             btnDelete.Enabled = True
151             btnSave.Enabled = False
152         Catch ex As Exception
153             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
154         End Try
155     End Sub
156
157     Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
158         Dim strRowNumber As String = (e.RowIndex +
1).ToString()
159         Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
160         If dgw.RowHeadersWidth < Convert.ToInt32((size.Width +
20)) Then
161             dgw.RowHeadersWidth = Convert.ToInt32((size.Width +
20))
162         End If
163         Dim b As Brush = SystemBrushes.ControlText
164         e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X +
15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
165
166     End Sub
167     Public Sub Getdata()
168         Try
169             con = New SqlConnection(cs)
170             con.Open()
171             cmd = New SqlCommand(
"SELECT RTRIM(Designation) from Designation order by Designation", con)
172             rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
173             dgw.Rows.Clear()
174             While (rdr.Read() = True)
175                 dgw.Rows.Add(rdr(
0))
176             End While
177             con.Close()
178         Catch ex As Exception
179             MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
180         End Try
181     End Sub
182
183     Private Sub frmDesignation_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
184         Getdata()
185     End Sub
186 End Class


Gõ tìm kiếm nhanh...